package com.big.cmn.controller;

import com.big.model.model.merchandise.MerchandiseDict;
import com.big.cmn.service.DictService;
import com.big.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@Api
@RestController
@RequestMapping("/api/cmn/dict")
//@CrossOrigin //跨域
@RefreshScope //动态刷新配置
@Slf4j
public class DictController {

    @Autowired
    private DictService dictService;

    //导入数据字典
    @PostMapping("importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();
    }

    //导出数据字典接口
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        dictService.exportDictData(response);
    }

    //根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode) {
        List<MerchandiseDict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id) {
        List<MerchandiseDict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value")  String value) {
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    //根据value查询
    @GetMapping("getName/{value}")
    public String getName(@PathVariable("value") String value) {
        String dictName = dictService.getDictName("",value);
        return dictName;
    }


    //省市区级联
    @ApiOperation(value = "省市区级联")
    @GetMapping("cascading/{id}")
    public Result cascading(@PathVariable("id") Long id) {
        List<Object> cascading = dictService.cascading(String.valueOf(id));
        return Result.ok(cascading);
    }



    @ApiOperation(value = "根据父name和子name 查parent_id地址名")
    @GetMapping("/parentIdByName/{parentName}/{name}")
    public Result parentIdByName(@PathVariable("parentName") String parentName, @PathVariable("name") String name) {
        MerchandiseDict parentCode = dictService.parentIdByName(parentName,name);
        return Result.ok(parentCode);
    }


    @ApiOperation(value = "根据地名name 查parent_id")
    @GetMapping("/dictCode/{name}")
    public Long dictCodeAddressId( @PathVariable("name") String name) {
        Long aLong = dictService.dictCodeAddressId(name);
        return aLong;
    }




 }
